package com.gitee.chessboard.jdbc;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

/**
 * Chessboard自定义的数据源
 *
 * @author 敲代码的旺财
 * @date 2021/11/18 13:16
 */
public class ChessboardDataSource implements DataSource {

    private final String driverClassName;
    private final String url;
    private final String userName;
    private final String password;

    public ChessboardDataSource(String driverClassName, String url, String userName, String password) throws ClassNotFoundException {
        this.driverClassName = driverClassName;
        this.url = url;
        this.userName = userName;
        this.password = password;
        loadDriver();
    }

    public void loadDriver() throws ClassNotFoundException {
        Class.forName(this.driverClassName);
    }

    @Override
    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(this.url, this.userName, this.password);
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return DriverManager.getConnection(this.url, username, password);
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}
